
package model;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * De klasse Level is verantwoordelijk voor:
 *      een lvl GENEREN die de klasse game kan gebruiken
 * 
 * Level heeft geen kennis van andere klassen
 * 
 * Author Misja, Bob.
 */

public class Level {

    private int[][] level;
    private ArrayList fields;
    private int FIELDSIZE;

    public Level() {
        
        this.FIELDSIZE = 30;
        
        level = new int[][]{
            {5, 1, 1, 1, 1, 3, 2, 1, 1, 1},
            {1, 3, 2, 1, 1, 1, 1, 3, 3, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 3, 2, 1, 1, 1, 1, 1, 3, 3},
            {1, 3, 1, 1, 3, 1, 2, 1, 1, 1},
            {1, 2, 1, 1, 2, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 3, 1, 1, 1, 1, 1},
            {3, 1, 1, 1, 1, 1, 3, 3, 2, 1},
            {3, 1, 3, 1, 1, 1, 1, 3, 3, 1},
            {1, 1, 3, 2, 3, 1, 1, 1, 1, 1},
         {1, 1, 1, 1, 1, 1, 1, 1, 1, 4}};
    }
    
    /*
     * Maakt het level aan via de level spec
     */
    public ArrayList<Field> generateLevel() {

        fields = new ArrayList<>();

        for (int row = 0; row < this.level.length; row++) {

            for (int column = 0; column < this.level[row].length; column++) {

                int xCoordinate = column * FIELDSIZE;
                int yCoordinate = row * FIELDSIZE;

                Field field = new Field(xCoordinate, yCoordinate);

                switch (this.level[row][column]) {
                    case 0:
                        break;
                    case 1:
                        fields.add(field);
                        break;
                    case 2:
                        
                        field.setPart(new Wall());
                        field.getPart().setCurrentField(field);
                        fields.add(field);
                        break;
                    case 3:
                        field.setPart(new Box());
                        field.getPart().setCurrentField(field);
                        fields.add(field);
                        break;
                    case 4:
                        field.setPart(new Enemy());
                        field.getPart().setCurrentField(field);
                        fields.add(field);
                        break;
                    case 5:
                        field.setPart(new Player());
                        field.getPart().setCurrentField(field);
                        fields.add(field);
                        break;
                }
            }
        }

        this.pairFields();
        return fields;
    }

    /*
     * Koppelt buren aan elkaar aan de hand van x en y coordinaten 
     */
    public void pairFields() {
        Iterator<Field> it = this.fields.iterator();
        while (it.hasNext()) {
            Field f = it.next();
            for (int i = 1; i <= 4; i++) {
                switch (i) {
                    case 1:
                        f.setNeighbor(findField((f.getxCoordinate() + FIELDSIZE), f.getyCoordinate()), "RIGHT");
                        break;
                    case 2:
                        f.setNeighbor(findField((f.getxCoordinate() - FIELDSIZE), f.getyCoordinate()), "LEFT");
                        break;
                    case 3:
                        f.setNeighbor(findField(f.getxCoordinate(), (f.getyCoordinate() + FIELDSIZE)), "DOWN");
                        break;
                    case 4:
                        f.setNeighbor(findField(f.getxCoordinate(), (f.getyCoordinate() - FIELDSIZE)), "UP");
                        break;
                }
            }
        }

    }

    /*
     * Vind een veld aan de hand van x en y coordinate
     */
    public Field findField(int xCoordinate, int yCoordinate) {

        Iterator<Field> it = this.fields.iterator();
        while (it.hasNext()) {
            Field f = it.next();
            if (f.getxCoordinate() == xCoordinate && f.getyCoordinate() == yCoordinate) {
                return f;
            }
        }
        return null;
    }
}
